package written.w20240908xiaohongshu.q1;

/**
 * Main
 *
 * @Author : Ruoyu Wang
 * @DateTime : 2024/9/8 15:57
 */

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        int n = scanner.nextInt();
        int m = scanner.nextInt();
        scanner.nextLine();

        char[][] g = new char[n][m];
        int[][] f = new int[n][m];

        for (int i = 0; i < n; i++) {
            String line = scanner.nextLine();
            for (int j = 0; j < m; j++) {
                g[i][j] = line.charAt(j);
                f[i][j] = 0;
            }
        }

        Map<Character, int[]> d = new HashMap<>();
        d.put('L', new int[]{0, -1});
        d.put('R', new int[]{0, 1});
        d.put('U', new int[]{-1, 0});
        d.put('D', new int[]{1, 0});

        Map<Integer, Boolean> map = new HashMap<>();
        int idx = 0;

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (f[i][j] == 0) {
                    idx++;
                    int x = i;
                    int y = j;
                    while (x >= 0 && x < n && y >= 0 && y < m && f[x][y] == 0) {
                        f[x][y] = idx;
                        int[] dir = d.get(g[x][y]);
                        x += dir[0];
                        y += dir[1];
                    }
                    if (x >= 0 && x < n && y >= 0 && y < m) {
                        if (f[x][y] == idx) {
                            map.put(idx, true);
                        } else {
                            map.put(idx, map.getOrDefault(f[x][y], false));
                        }
                    } else {
                        map.put(idx, false);
                    }
                }
            }
        }

        int res = 0;

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (map.getOrDefault(f[i][j], false)) {
                    res++;
                }
            }
        }

        System.out.println(res);
        scanner.close();
    }
}
